package com.yvan.springmvc;

import com.google.common.collect.Maps;
import com.yvan.platform.ExceptionCode;
import com.yvan.platform.Pagination;
import com.yvan.platform.StringUtils;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * 用来快速返回结果的model对象
 */
public class ResultModel extends LinkedHashMap<String, Object> implements Serializable {

    public static ResultModel newSuccess() {
        return new ResultModel().success(true);
    }

    public static ResultModel newSuccess(final Object data) {
        return new ResultModel().success(true).putData(data);
    }

    public static ResultModel newFail(final String errorMsg) {
        return new ResultModel().success(false).putMsg(errorMsg);
    }

    public static ResultModel newFail(final String errorMsg, String state) {
        return newFail(errorMsg).put("state", state);
    }

    public static ResultModel newFailActivity(final String data) {
        return new ResultModel().success(false).putData(data);
    }

    public static ResultModel newBootstrapPaginationData(final Pagination pagination, final Object data) {
        return new ResultModel().success(true).putTotal(pagination.getCount()).putRows(data);
    }

    public static Object newEasyuiResult(final int total, final Object rows) {
        return new ResultModel().putTotal(total).putRows(rows);
    }

    // 移动端接口返回固定格式
    public static ResultModel mobileReturnMap(boolean success, String message, Object data, String errCode) {
        return new ResultModel().putMobileReturnMap(success, message, data, errCode);
    }

    // 移动端接口返回固定格式
    public static ResultModel mobileReturnMap(boolean success, String message, Object data) {
        return new ResultModel().putMobileReturnMap(success, message, data, null);
    }

    // 移动端接口返回固定格式
    public static ResultModel mobileReturnError(String message, String errCode) {
        return new ResultModel().putMobileReturnMap(false, message, Maps.newHashMap(), errCode);
    }

    // 移动端接口返回固定格式
    public static ResultModel mobileReturnMap(boolean success, String message) {
        return new ResultModel().putMobileReturnMap(success, message, Maps.newHashMap(), null);
    }

    // 移动端接口返回固定格式
    public static ResultModel resultMap(int status, String msg, Object data) {
        return new ResultModel().putBaseResultMap(status, msg, data);
    }

    //app返回模板
    public static ResultModel appReturnMap(int status, String msg, String errorMsg, Object data) {
        return new ResultModel().putAppReturnMap(status, msg, errorMsg, data);
    }

    //app返回无返回参数
    public static ResultModel appReturnMap(int status, String msg) {
        return new ResultModel().putAppReturnMap(status, msg, null, null);
    }

    //app返回有返回参数
    public static ResultModel appReturnMap(int status, String msg, Object data) {
        return new ResultModel().putAppReturnMap(status, msg, null, data);
    }

    //app返回请求后台报错
    public static ResultModel appReturnError(int status, String msg, String errorMsg) {
        return new ResultModel().putAppReturnMap(status, msg, errorMsg, null);
    }

    //aap返回请求成功
    public static ResultModel appReturnOk() {
        return new ResultModel().putAppReturnMap(1, "请求成功", null, null);
    }

    public ResultModel putMsg(String msg) {
        return set("msg", msg);
    }

    public ResultModel putData(Object data) {
        return set("data", data);
    }

    public ResultModel putTotal(int total) {
        return set("total", total);
    }

    public ResultModel putRows(Object rows) {
        return set("rows", rows);
    }

    public ResultModel success(boolean success) {
        return set("success", success).set("state", ExceptionCode.SUCCESS);
    }

    public ResultModel set(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public ResultModel put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    // 移动端接口返回固定格式
    private ResultModel putMobileReturnMap(boolean success, String message, Object data, String errCode) {
        return set("success", success).set("message", message).set("data", data).set("errCode", errCode);
    }

    // 移动端接口返回固定格式
    private ResultModel putBaseResultMap(int status, String msg, Object data) {
        return set("status", status).set("msg", msg).set("data", data);
    }

    //app返回固定格式
    private ResultModel putAppReturnMap(int status, String msg, String errorMsg, Object data) {
        return set("status", status).set("msg", msg).set("data", StringUtils.isNullOrEmpty(data) ? null : data);
    }

    public void putAppReturnMap(ResultModel resultModel, Boolean removeKey) {
        ResultModel resultMap = new ResultModel();
        if (null != resultModel && !resultModel.isEmpty()) {
            for (String key : resultModel.keySet()) {
                resultMap.put(key, resultModel.get(key));
            }
            if (removeKey) {
                resultMap.remove("status");
                resultMap.remove("msg");
            }
        }
        resultModel.put("data", resultMap);
    }

    public void putAppReturnMap(ResultModel resultModel) {
        putAppReturnMap(resultModel, true);
    }

}
